/**
 * Created on 2007-03-15 05:48
 */
package game;

import java.awt.Color;
import java.awt.Font;

import media.gfx.RenderManager;

/**
 * @author David Wallin
 * 
 * An entity displaying a text at a certain location
 */
public class TextEntity extends Entity
{
	/**
	 * The text to put where the dynamic text should be in the static one
	 */
	public static final String		DYNAMIC_TEXT = "%!DYNAMIC!%";
	/**
	 * This text is always shown, and first.. And can only be set in the constructor, and never changed
	 */
	private final String		m_StaticText;
	private String				m_DynamicText;
	
	private Font				m_Font;
	private Color				m_Color;
	
	private RenderManager		m_RenderManager = RenderManager.INSTANCE;
	
	/**
	 * 
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 */
	public TextEntity(int xpos, int ypos, int width, int height)
	{
		this("", "", RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 */
	public TextEntity(String staticText, int xpos, int ypos, int width, int height)
	{
		this(staticText, "", RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 */
	public TextEntity(String staticText, String dynamicText, int xpos, int ypos, int width, int height)
	{
		this(staticText, dynamicText, RenderManager.SERIF_12_ITALIC, Color.white, xpos, ypos, width, height);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param font the font this entity should use to display its text
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 */
	public TextEntity(String staticText, String dynamicText, Font font, int xpos, int ypos, int width, int height)
	{
		this(staticText, dynamicText, font, Color.white, xpos, ypos, width, height);
	}
	
	/**
	 * 
	 * @param staticText the text this entity should always display (this can never be changed)
	 * @param dynamicText the text this entity should display (this text can be changed)
	 * @param font the font this entity should use to display its text
	 * @param color the color this entity should use to display its text
	 * @param xpos the x position this entity should get
	 * @param ypos the y position this entity should get
	 * @param width the width this entity should get
	 * @param height the height this entity should get
	 */
	public TextEntity(String staticText, String dynamicText, Font font, Color color, int xpos, int ypos, int width, int height)
	{
		super(xpos, ypos, width, height);
		m_StaticText = staticText;
		m_DynamicText = dynamicText;
		m_Font = font;
		m_Color = color;
	}
	
	/**
	 * Sets the text to display efter the static text
	 * 
	 * @param dynamicText the text to display
	 */
	public void setDynamicText(String dynamicText)
	{
		m_DynamicText = dynamicText;
	}
	
	/**
	 * Sets the font in which to display the text
	 * 
	 * @param font the font that this TextEntity should use
	 */
	public void setFont(Font font)
	{
		m_Font = font;
	}
	
	/**
	 * Sets the color in which to display the text
	 * 
	 * @param color the color that this TextEntity should use
	 */
	public void setColor(Color color)
	{
		m_Color = color;
	}
	
	/* (non-Javadoc)
	 * @see game.Entity#draw()
	 */
	@Override
	protected void doDraw()
	{
		String text = m_StaticText + m_DynamicText;
		
		if(m_StaticText.contains(DYNAMIC_TEXT))
		{
			text = m_StaticText.replaceFirst(DYNAMIC_TEXT, m_DynamicText);
		}
		
		m_RenderManager.drawString(text, getXpos(), getYpos(), m_Font, m_Color);
	}
}
